package jrobutt.room.level;

import jrobutt.*;

import java.awt.Graphics2D;

public class FinalGuyHandler implements Updatable
{
    public Guy guy;
    public TerrainGrid terrain;
    public OilGrid oil_grid;
    
    public Vector2 final_direction;
    
    public boolean goto_next_room;
    
    private boolean active;
    
    /* assume initial_position != final_position */
    public FinalGuyHandler (Guy guy, Vector2 final_direction, TerrainGrid terrain, OilGrid oil_grid)
    {
        this.guy = guy;
        this.final_direction = final_direction;
        
        this.terrain = terrain;
        this.oil_grid = oil_grid;
        
        goto_next_room = false;
        
        active = false;
    }
    
    public void update ()
    {
        if (!active) return;
        
        float level_width = terrain.width * terrain.scale;
        float level_height = terrain.height * terrain.scale;
        if (!(guy.x - guy.radius < level_width &&
              guy.x + guy.radius >= 0.0f &&
              guy.y - guy.radius < level_height &&
              guy.y + guy.radius >= 0.0f))
        {
            goto_next_room = true;
        }
    }
    public void activate ()
    {
        if (active) return;
        
        active = true;
        guy.can_move = false;
        
        Vector2 d = final_direction;
        guy.facing_direction.set(d);
        guy.xspeed = d.x * guy.move_spd;
        guy.yspeed = d.y * guy.move_spd;
        
        terrain.close_exit_gate();
        
        for (TerrainGrid.Block block : terrain.blocks)
        {
            if (block == null) continue;
            
            if (block.datum == TerrainGrid.EXIT_GATE_DATUM)
            {
                oil_grid.remove_oil_within(block.x, block.y, block.getWidth(), block.getHeight());
            }
        }
    }
}
